package com.example.yls.demoa;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.nfc.Tag;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "TAG";
    //    private static final String TAG = "TAG";
    private WebView my_webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_webview = findViewById(R.id.my_webview);
        WebSettings webSettings = my_webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");

        my_webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
               Log.e(TAG,"alert message = " + message);
               Uri uri = Uri.parse(message);
                if (uri.getScheme().equals("js")) {
                    if (uri.getAuthority().equals("webview")) {
                        String func = uri.getQueryParameter("func");
                        if (func.equals("showDetail")) {
                            String id = uri.getQueryParameter("id");
                            Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                            intent.putExtra("id", id);
                            startActivity(intent);
                        }
                    }
                }

                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                Uri uri = Uri.parse(message);
                if (uri.getScheme().equals("js")) {
                    if (uri.getAuthority().equals("webview")) {
                        String func = uri.getQueryParameter("func");
                        if (func.equals("getInfo")) {
                            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                            .setTitle("请选择性别")
                                    .setPositiveButton("帅哥", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                        result.confirm();
                                        dialogInterface.cancel();
                                        }
                                    })
                                    .setNegativeButton("美女", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            result.cancel();
                                            dialogInterface.cancel();
                                        }
                                    }).create();
                            alertDialog.show();


                        }
                    }
                }

                return true;
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
                  Uri uri = Uri.parse(message);
                  if (uri.getScheme().equals("js")){
                      if (uri.getAuthority().equals("webview")){
                          String func = uri.getQueryParameter("func");
                          if (func.equals("getName")){
                              final EditText editText = new EditText(MainActivity.this);
                             AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                                     .setTitle("请输入姓名")
                                     .setView(editText)
                                     .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                         @Override
                                         public void onClick(DialogInterface dialogInterface, int i) {
                                             String name = editText.getText().toString();
                                             result.confirm(name);
                                             dialogInterface.cancel();
                                         }
                                     }).create();
                             alertDialog.show();
                          }
                      }
                  }



                return true;
            }
        });



        my_webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,
                                                    String url) {
                Log.e(TAG, "url = " + url);
                Uri uri = Uri.parse(url);
                Log.e(TAG, "sheme = " + uri.getScheme());
                Log.e(TAG, "Authority = " + uri.getAuthority());


                if (uri.getScheme().equals("js")) {
                    if (uri.getAuthority().equals("webview")) {
//                        Set<String> paramSet = uri.getQueryParameterNames();
//                        for (String parm : paramSet) {
//                            String value = uri.getQueryParameter(parm);
//                            Log.e(TAG, "param = " + parm + "value = " + value);
//                        }
                        String func = uri.getQueryParameter("func");
                        if (func.equals("showDetail")) {
                            String id = uri.getQueryParameter("id");
                            Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                            intent.putExtra("id", id);
                            startActivity(intent);
                        }
                    }
                }
                return true;
            }
        });

        my_webview.loadUrl("file:///android_asset/test.html");


    }
}
